#Requires AutoHotkey v2.0
#SingleInstance force

#Include ../utils/base.ahk
#Include ../utils/expect.ahk

XU := XUtils()
XF := XFile()
TEST_COM_OBJECT := ComObject("WIA.ImageFile")
; img_file.LoadFile("debug_image.png")
class TestObject {
    name := "John"
    age := 16
    height := 1.72
}
TEST_OBJECT := TestObject()
TEST_ARRAY := [10, 3.14159, "hello"]

main_test_base()

main_test_base() {
    assert := expect()

    assert.group("XUtils.is*")
    assert.label("isString")
    assert.true(XU.isString("abc"))
    assert.false(XU.isString(123))
    assert.false(XU.isString(map("a", "b")))

    assert.label("isNumber")
    assert.true(XU.isNumber(11))
    assert.true(XU.isNumber(11.12))
    assert.false(XU.isNumber("11"))
    assert.false(XU.isNumber(map("a", "b")))

    assert.label("isArray")
    assert.true(XU.isArray(["a", "b"]))
    assert.false(XU.isArray(map("a", "b")))
    assert.false(XU.isArray("abc"))

    assert.label("isMap")
    assert.true(XU.isMap(map("a", "b")))
    assert.false(XU.isMap(["a", "b"]))
    assert.false(XU.isMap("abc"))

    assert.label("isInteger")
    assert.true(XU.isInteger(123))
    assert.false(XU.isInteger("123"))
    assert.false(XU.isInteger(1.23))

    assert.label("isFloat")
    assert.true(XU.isFloat(1.23))
    assert.false(XU.isFloat(123))
    assert.false(XU.isFloat("123"))

    assert.label("isComObject")
    assert.true(XU.isComObject(TEST_COM_OBJECT))
    assert.false(XU.isComObject(123))
    assert.false(XU.isComObject("123"))
    assert.false(XU.isComObject(TEST_OBJECT))

    assert.group("XUtils.num*")
    assert.equal(XU.round(1.623), 2)
    assert.equal(XU.round(1.13), 1)
    assert.equal(XU.round(1.1312, 2), 1.13)

    assert.equal(XU.fromBase("ab", 16), 171)
    assert.equal(XU.fromBase("111", 16), 273)
    assert.equal(XU.fromBase("421", 8), 273)
    assert.equal(XU.fromBase("101", 2), 5)

    assert.equal(XU.toBase(15, 16), "F")
    assert.equal(XU.toBase(1011, 16), "3F3")
    assert.equal(XU.toBase(1011, 8), "1763")
    assert.equal(XU.toBase(1011, 2), "1111110011")

    assert.group("XUtils.str*")
    assert.label("toString")
    assert.equal(XU.toString(map("a", "b", "cc", 11, "d", map("11", "22"), "f", ["1", 2, map(12, "23")])), "{'a': 'b', 'cc': 11, 'd': {'11': '22'}, 'f': ['1', 2, {12: '23'}]}")
    assert.equal(XU.toString(TEST_COM_OBJECT), "<Class 'ImageFile'>")
    assert.equal(XU.toString(TEST_OBJECT), "<Object 'TestObject', age=16, height=1.72, name='John'>")

    assert.label("join")
    assert.equal(XU.join(["hello", "world", "autohotkey"]), "hello world autohotkey")
    assert.equal(XU.join([11, "22", 33], ", "), "11, 22, 33")

    assert.label("format")
    assert.equal(XU.format("{}", "hello", "world"), "hello")
    assert.equal(XU.format("{}, {}", "hello", TEST_COM_OBJECT), "hello, <Class 'ImageFile'>")

    assert.group("XUtils.arr*")
    assert.label("indexOf")
    assert.equal(XU.indexOf(TEST_ARRAY, "hello"), 3)
    assert.equal(XU.indexOf(TEST_ARRAY, "john"), -1)
    assert.equal(XU.indexOf(TEST_ARRAY, "john", "not"), "not")

    assert.group("XUtils.regex")
    assert.label("toString")
    assert.equal(XU.toString(XU.findall("111%a%%ccc%33333", "%([^%]*)%")), "[<Object 'RegExMatchInfo', Enums: 0='%a%', 1='a'>, <Object 'RegExMatchInfo', Enums: 0='%%', 1=''>, <Object 'RegExMatchInfo', Enums: 0='%ccc%', 1='ccc'>]")

    assert.group("XUtils.random")
    assert.label("random")
    assert.true(XU.rnd() >= 0 and XU.rnd() <= 1)
    assert.true(XU.rnd(10, 20) >= 10 and XU.rnd(10, 20) <= 20)

    assert.label("rand_choice")
    assert.true(XU.indexOf(TEST_ARRAY, XU.rnd_choice(TEST_ARRAY*)) > 0)

    assert.label("rand_sample")
    assert.true(XU.len(XU.rnd_sample(TEST_ARRAY)) = 3)
    assert.true(XU.len(XU.rnd_sample(TEST_ARRAY, 2)) = 2)

    assert.group("XUtils.utils")
    assert.label("len")
    assert.equal(XU.len("hello"), 5)
    assert.equal(XU.len(123), 3)
    assert.equal(XU.len([3, 5, 7]), 3)
    assert.equal(XU.len(map('a', 2, 'b', 5)), 2)

    XU.print('try print:', [3, 5, 7])
    XU.print('try print:', map('a', 2, 'b', 5), 2, "nihao")

    assert.label("now")
    assert.equal(XU.len(XU.now()), 17)
    assert.equal(XU.len(XU.toString(XU.now(true))), 16)

    assert.group("XFile.is*")
    assert.label("detect")
    assert.equal(XF.exist("debug_image.png"), "A")
    assert.equal(XF.exist("../debug_image.png"), "")
    assert.true(XF.is_file("debug_image.png"))
    assert.true(XF.is_file("../LICENSE"))
    assert.false(XF.is_file("../tests"))
    assert.false(XF.is_dirt("debug_image.png"))
    assert.false(XF.is_dirt("../LICENSE"))
    assert.true(XF.is_dirt("../tests"))

    assert.group("Report Test")
    ; assert.fullReport()
}